package com.mcxx.modules.miniSalvation.family.service.read;

import com.mcxx.constant.DictConstant;
import com.mcxx.modules.base.service.read.AreaReadService;
import com.mcxx.modules.base.service.read.DictionaryReadService;
import com.mcxx.modules.miniSalvation.family.dao.read.FamilyMainReadDao;
import com.mcxx.modules.miniSalvation.family.dto.*;
import com.mcxx.modules.miniSalvation.family.entity.FamilyMainBackUpEntity;
import com.mcxx.modules.miniSalvation.family.entity.FamilyMainEntity;
import com.mcxx.modules.miniSalvation.family.param.FamilyMemberParam;
import com.mcxx.modules.miniSalvation.order.entity.OrderEntity;
import com.mcxx.result.BaseResult;
import com.mcxx.util.CommonUtil;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.component.org.orguser.area.entity.TaAreaPo;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import com.yinhai.ta404.module.dict.entity.TaDictPo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
@TaTransactional(readOnly = true)
public class FamilyMainReadService extends BaseService {
    @Autowired
    private FamilyMainReadDao familyMainReadDao;
    @Autowired
    private AreaReadService areaReadService;
    @Autowired
    private DictionaryReadService dictionaryReadService;


    public FamilyMainEntity getById(String id) {
        return familyMainReadDao.getById(id);
    }

    public Integer getFamilySaveNumber(String familyId) {
        return familyMainReadDao.getFamilySaveNumber(familyId);
    }



    public BaseResult<FamilyMainListInfoDTO> getFamilyMainByOrderId(String orderId) {

        BaseResult<FamilyMainListInfoDTO> rstMsg = new BaseResult<FamilyMainListInfoDTO>();
        if(CommonUtil.invalidArgs(orderId)){
            rstMsg.setCode(FailureCode.ERR_10000.getCode());
            rstMsg.setMsg(FailureCode.ERR_10000.getMessage());
            return rstMsg;
        }

        FamilyMainListInfoDTO familyMain = familyMainReadDao.queryFamilyMainByOrderId(orderId);

        TaAreaPo taAreaPo5 = areaReadService.getAreaByCode(familyMain.getAreaCode());
        familyMain.setAreaCode(taAreaPo5.getAreaName());

        TaDictPo povertyFactorsDict = dictionaryReadService.getCodeCache(DictConstant.POVERTY_FACTORS, familyMain.getMainReason());
        TaDictPo familyRelationDict = dictionaryReadService.getCodeCache(DictConstant.FM_RELATION, familyMain.getFamilyRelation());
        familyMain.setMainReason(povertyFactorsDict.getLabel());
        familyMain.setFamilyRelation(familyRelationDict.getLabel());

        rstMsg.setData(familyMain);
        return  rstMsg;
    }



    public Page queryMemberByAreaCodeState(FamilyMemberParam param){
        List<FamilyMainEntity> list = familyMainReadDao.queryMemberByAreaCodeState(param);
        return familyMainReadDao.endPager(list);
    }


    public String getAreaCodeById(String id) {
        return familyMainReadDao.getAreaCodeById(id);
    }


    public String getFamilyIdByMemberIdCard(String idcard) {
        return familyMainReadDao.getFamilyIdByMemberIdCard(idcard);
    }

    public FamilyMainBackUpEntity selectBackUpInfo(String orderId){
        return familyMainReadDao.selectBackUpInfo(orderId);
    }

    public String selectFamilyRevenue(String familyId){
        return familyMainReadDao.selectFamilyRevenue(familyId);
    }

    public String selectCalculateHtml(String familyId){
        return familyMainReadDao.selectCalculateHtml(familyId);
    }

    public String selectPhoneByFamilyId(String familyId){return familyMainReadDao.selectPhoneByFamilyId(familyId);}


    public Integer getByMemberIdCard(String idcard) {
        return familyMainReadDao.getByMemberIdCard(idcard);
    }

    public String getLowerFamilyIdByMemberIdCard(String idcard){
        return familyMainReadDao.getLowerFamilyIdByMemberIdCard(idcard);
    }

    public String selectSaveTypeByFamilyId(String familyId){
        return familyMainReadDao.selectSaveTypeByFamilyId(familyId);
    }


    public FamilyMainEntity getLowerFamilyById(String familyId) {
        return familyMainReadDao.getLowerFamilyById(familyId);
    }


    public OrderEntity getOrderInfoByIdCard(String idCard) {
        return familyMainReadDao.getOrderInfoByIdCard(idCard);
    }


    public OrderEntity getLowerOrderInfoByIdCard(String idCard) {
        return familyMainReadDao.getLowerOrderInfoByIdCard(idCard);
    }


    public List<Map<String,String>> getMemberByIdCards(List<String> memberIdCards) {
        return familyMainReadDao.getMemberByIdCards(memberIdCards);
    }


    public List<Map<String,String>> getLowerMemberByIdCards(List<String> memberIdCards) {
        return familyMainReadDao.getLowerMemberByIdCards(memberIdCards);
    }
}
